package com.yili.oa.service.impl;

import com.yili.oa.dao.RewardHistoryMapper;
import com.yili.oa.dao.extend.FtBetRecordExtendMapper;
import com.yili.oa.dao.extend.UserExtendMapper;
import com.yili.oa.entity.ExtractsRecordSearch;
import com.yili.oa.entity.FtBetRecordSearch;
import com.yili.oa.models.*;
import com.yili.oa.service.ExtractsRecordService;
import com.yili.oa.service.FtBetRecordService;
import com.yili.oa.utils.Exception.AjaxException;
import com.yili.oa.utils.ListUtils;
import com.yili.oa.utils.annotate.DataBase;
import com.yili.oa.vo.response.AccountStatisticsRes;
import com.yili.oa.vo.response.FtBetRecordRes;
import com.yili.oa.vo.special.FtBetRecordSpec;
import com.yili.oa.vo.special.RewardHistorySpec;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

@SuppressWarnings("ALL")
@Service
@DataBase("lyt_game_db")
@Transactional(value = "transactionManager")
public class FtBetRecordServiceImpl implements FtBetRecordService {

    @Autowired
    FtBetRecordExtendMapper mapper;

    @Autowired
    UserExtendMapper userExtendMapper;

    @Autowired
    private ExtractsRecordService extractsRecordService;

    @Autowired
    private RewardHistoryMapper rewardHistoryMapper;



    @Override
    public Integer add(FtBetRecord ftBetRecord) {
        long time = new Date().getTime();
        ftBetRecord.setCreateTimeUtc(time);
        ftBetRecord.setBetNo(time+"-"+randomBetNo());
        return mapper.insert(ftBetRecord);
    }

    @Override
    public List<FtBetRecordRes> get(FtBetRecordSearch searchBase) {
        List<FtBetRecordRes> list = mapper.get(searchBase);
        return list;
    }

    @Override
    public void update(FtBetRecord ftBetRecord) {
        long time = new Date().getTime();
        ftBetRecord.setPayoutTimeUtc(time);
        mapper.updateByPrimaryKeySelective(ftBetRecord);

    }


    public String randomBetNo(){
        String num = "";
        Random r = new Random();
        for (int i = 0; i < 8; i++) {
            num+=r.nextInt(10);
        }
        return num;
    }


    public AccountStatisticsRes getAccountStatistics(FtBetRecordSearch searchBase){
        AccountStatisticsRes accountStatistics = mapper.getAccountStatistics(searchBase);
        BigDecimal bigDecimal = accountStatistics.getAccumulateBet().add(accountStatistics.getPieAwarded());
        accountStatistics.setEarnings(bigDecimal);
        ExtractsRecordSearch extractsRecordSearch = new ExtractsRecordSearch();
        extractsRecordSearch.setBeginDate(new Date(searchBase.getBegin()));
        extractsRecordSearch.setEndDate(new Date(searchBase.getEnd()));
        extractsRecordSearch.setStatus(2);
        List<ExtractsRecord> extractsRecords = extractsRecordService.get(extractsRecordSearch);
        if(ListUtils.isNullOrEmpty(extractsRecords)){
            accountStatistics.setRebate(BigDecimal.ZERO);
        }else{
            BigDecimal[] zero = new BigDecimal[]{BigDecimal.ZERO};
            extractsRecords.forEach(extractsRecord->{
                zero[0] = zero[0].add(extractsRecord.getAmount());
            });
            accountStatistics.setRebate(zero[0]);
        }
        return accountStatistics;
    }

    @Override
    public List<FtBetRecordSpec> getByUserId(Long userId,Long gameKind,Date datetime) {
        List<FtBetRecordSpec> list = new ArrayList<>();
        if (datetime==null) {
            list = mapper.getRewardRecord(userId, gameKind, null);
        }else {
            list = mapper.getRewardRecord(userId, gameKind, datetime);
        }
        return list;
    }

    @Override
    public List<FtBetRecordSpec> getByGameId(Long userId, Long gameKind,Date datetime) {
        List<FtBetRecordSpec> list = new ArrayList<>();
        if (datetime==null) {
            list = mapper.getRewardRecord2(userId, gameKind, null);
        }else {
            list = mapper.getRewardRecord2(userId, gameKind, datetime);
        }
        return list;
    }

    @Override
    public Float getBetSum(Long userId) {
        BigDecimal bigDecimal = mapper.getBetSum(userId);
        return bigDecimal.floatValue();
    }

    @Override
    public List<FtBetRecord> getByUserId(Long userId) {
        FtBetRecordExample example = new FtBetRecordExample();
        FtBetRecordExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(userId);
        criteria.andXimaTimeIsNull();
        return mapper.selectByExample(example);
    }

    @Override
    public void add(RewardHistorySpec rewardHistorySpec) {
        RewardHistory rewardHistory = new RewardHistory();
        rewardHistory.setRewardTime(rewardHistorySpec.getRewardTime());
        rewardHistory.setRewardSum(rewardHistorySpec.getRewardSum());
        rewardHistory.setRewardMoney(rewardHistorySpec.getRewardMoney());
        try {
            rewardHistoryMapper.insert(rewardHistory);
        }catch (Exception e){
            throw new AjaxException("添加失败，请重试");
        }
    }

    @Override
    public List<RewardHistorySpec> getRewarHist(Long userId) {
        RewardHistoryExample example = new RewardHistoryExample();
        RewardHistoryExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(userId);
        example.setOrderByClause("reward_time DESC");
        List<RewardHistory> list = rewardHistoryMapper.selectByExample(example);
        List<RewardHistorySpec> result = new ArrayList<>();
        for (RewardHistory rewardHistory:
             list) {
            RewardHistorySpec rewardHistorySpec = new RewardHistorySpec();
            BeanUtils.copyProperties(rewardHistory,rewardHistorySpec);
            result.add(rewardHistorySpec);
        }
        return result;
    }

}
